package com.anjho.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.AjaxBehaviorEvent;
import javax.persistence.NoResultException;

import com.anjho.dao.DAOAddress;
import com.anjho.dao.DAOProfile;
import com.anjho.dao.DAOUser;
import com.anjho.pojo.Address;
import com.anjho.pojo.PersonType;
import com.anjho.pojo.Profile;
import com.anjho.pojo.User;

@ManagedBean(name = "userBean")
@SessionScoped
public class UserBean extends GenericBean implements Serializable {

	private static final long serialVersionUID = -7884871254823505962L;

	private List<User> userList;
	private List<Address> addressList;
	private List<Profile> profileList;
	private User user;
	private Address address;
	private Profile profile;

	private String cpf;
	private String name;
	private int addNumber;
	private String complement;
	private Date birthDate;
	private String phone1;
	private String phone2;
	private String email;

	private long addCode;
	private int proCode;

	private String login;
	private String password;
	private Boolean active;

	public UserBean() {
		super();
		clearForm();
	}

	public void clearForm() {
		this.setUser(new User());
		this.setUserList(new ArrayList<User>());
		this.setAddressList(new ArrayList<Address>());
		this.user = new User();
		this.setAddress(new Address());
		this.setCpf("");
		this.setName("");
		this.addNumber = 0;
		this.complement = "";
		this.birthDate = new Date();
		this.phone1 = "";
		this.phone2 = "";
		this.email = "";
		this.addCode = 0;
		this.login = "";
		this.password = "";
		this.active = true;
	}

	public List<User> getUserList() {
		synchronized (this) {
			DAOUser daoUser = new DAOUser();
			try {
				this.userList = daoUser.getUserList();
			} catch (NoResultException e) {
				this.userList = new ArrayList<User>();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return userList;
		}
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public List<Address> getAddressList() {
		return addressList;
	}

	public void setAddressList(List<Address> addressList) {
		this.addressList = addressList;
	}

	public String getCpf() {
		return cpf;
	}

	public void setCpf(String cpf) {
		this.cpf = cpf;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAddNumber() {
		return addNumber;
	}

	public void setAddNumber(int addNumber) {
		this.addNumber = addNumber;
	}

	public String getComplement() {
		return complement;
	}

	public void setComplement(String complement) {
		this.complement = complement;
	}

	public Date getBirthDate() {
		return birthDate;
	}

	public void setBirthDate(Date birthDate) {
		this.birthDate = birthDate;
	}

	public String getPhone1() {
		return phone1;
	}

	public void setPhone1(String phone1) {
		phone1 = phone1.replaceAll("[^0-9]", "");
		this.phone1 = phone1;
	}

	public String getPhone2() {
		return phone2;
	}

	public void setPhone2(String phone2) {
		phone2 = phone2.replaceAll("[^0-9]", "");
		this.phone2 = phone2;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public long getAddCode() {
		return addCode;
	}

	public void setAddCode(long addCode) {
		this.addCode = addCode;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Boolean isActive() {
		return active;
	}

	public void setActive(Boolean active) {
		this.active = active;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	public int getProCode() {
		return proCode;
	}

	public void setProCode(int proCode) {
		this.proCode = proCode;
	}

	public Profile getProfile() {
		return profile;
	}

	public void setProfile(Profile profile) {
		this.profile = profile;
	}

	public List<Profile> getProfileList() {
		synchronized (this) {
			DAOProfile daoProfile = new DAOProfile();
			try {
				this.profileList = daoProfile.getProfileList();
			} catch (NoResultException e) {
				this.profileList = new ArrayList<Profile>();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return profileList;
		}
	}

	public void selAddressChanged(AjaxBehaviorEvent event) {
		try {
			if (this.getAddCode() != 0) {
				DAOAddress daoAddress = new DAOAddress();
				this.setAddress(daoAddress.getAddressByAdCode(this.getAddCode()));
				daoAddress = null;
			} else {
				this.setAddress(new Address());
			}
		} catch (Exception ex) {
			FacesMessage facesMsg = new FacesMessage(
					FacesMessage.SEVERITY_FATAL, "Erro", ex.getMessage());
			FacesContext.getCurrentInstance().addMessage(null, facesMsg);
		}
	}

	public void selProfileChanged(AjaxBehaviorEvent event) {
		try {
			if (this.getProCode() != 0) {
				DAOProfile daoProfile = new DAOProfile();
				this.setProfile(daoProfile.getProfileByProCode(this
						.getProCode()));
			} else {
				this.setProfile(new Profile());
			}
		} catch (Exception ex) {
			FacesMessage facesMsg = new FacesMessage(
					FacesMessage.SEVERITY_FATAL, "Erro", ex.getMessage());
			FacesContext.getCurrentInstance().addMessage(null, facesMsg);
		}
	}

	public void onRegisterClick() throws Exception {
		try {
			User aux = new User();

			aux.getPerson().setAddress(this.getAddress());
			aux.getPerson().setAddressNumber(this.getAddNumber());
			aux.getPerson().setDocument(this.getCpf().replaceAll("[^0-9]", ""));
			aux.getPerson().setName(this.getName());
			aux.getPerson().setPersonType(new PersonType(1, "Física"));
			aux.getPerson().setBirthDate(this.getBirthDate());
			aux.getPerson().setEmail(this.getEmail());
			aux.getPerson().setComplement(this.getComplement());
			aux.getPerson().setPhone1(this.getPhone1());
			aux.getPerson().setPhone2(this.getPhone2());

			aux.setActive(this.isActive());
			aux.setLogin(this.getLogin());
			aux.setPassword(this.getPassword());
			aux.setProfile(this.getProfile());

			DAOUser daoUser = new DAOUser();
			daoUser.insert(aux);
			this.clearForm();
		} catch (Exception ex) {
			FacesMessage facesMsg = new FacesMessage(
					FacesMessage.SEVERITY_FATAL, "Erro", ex.getMessage());
			FacesContext.getCurrentInstance().addMessage(null, facesMsg);
		}
	}

}
